package com.yangcangclub.trolley.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yangcangclub.merchandise.bean.Product;
import com.yangcangclub.merchandise.domain.ProductBaseDomain;
import com.yangcangclub.trolley.base.TrolleyBaseService;
import com.yangcangclub.trolley.bean.ShoppingTrolley;
import com.yangcangclub.trolley.service.TrolleyService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 购物车serviceImpl
 * Created by 肖明明 on 2017/2/27.
 */
@Service
public class TrolleyServiceImpl extends TrolleyBaseService implements TrolleyService {

    /**
     * 添加到购物车，带数量
     */
    @Override
    public int addToTrolley(int pid, int userId, int num) {
        //查询是否添加过
        ShoppingTrolley st=  trolleyBasisDomain.selectShoppingTrolley(userId,pid);
        if(st!=null){//添加过，数量甲加若干
           return trolleyBasisDomain.changeNumOfProductInTrolley(userId,st.getId(),num+st.getNum());
        } else{//没添加，添加
            return trolleyBasisDomain.addToTrolley(pid, userId, num);
        }
    }
    
    /**
     * 根据购物车ID列表批量删除购物车商品
     */
    @Override
    public void delProductFromTrolley(int userId, List<Integer> tids) {
        trolleyBasisDomain.delProductFromTrolley(userId,tids);
    }

    /**
     * 查询用户未删除的购物车商品分页列表，可以用pageNum
     */
    @Override
    public Map<String, Object> getTrolleyProductsByPage(int userId, int pageNum, int pageSize) {
        Map rm=Maps.newHashMap();
        rm.put("list",Lists.newArrayList());
        rm.put("total",0);
        rm.put("curPageNum",pageNum);
        rm.put("totalPageNum",0);
        pageNum=pageNum>1?pageNum:1;
        int start=(pageNum-1)*pageSize;
        List<Map<String, Object>> idNumList=trolleyBasisDomain.getTrolleyProductsByPage(userId,start,pageSize);
        if(idNumList==null||idNumList.size()==0) return rm;
        //取总数 
        long num=trolleyBasisDomain.getTrolleyProductsByPageTotalNum(userId);
        List<Integer> pids=Lists.newArrayList();
        for(int i=0;i<idNumList.size();i++){//提取信息
            Map m=idNumList.get(i);
            pids.add((Integer)m.get("productId"));
        }
        //获取商品信息
        List<Product> productList=productBaseDomain.getProductListByIds(pids);
        if(productList==null||productList.size()==0)return rm;
        Map<Integer,Product> pMap=Maps.newHashMap();
        for(int i=0;i<productList.size();i++){//转换map
            pMap.put(productList.get(i).getId(),productList.get(i));
        }
        for(int i=0;i<idNumList.size();i++){//追加拼接商品信息
            Map m=idNumList.get(i);
            int pid=(int)m.get("productId");
            Product pp=pMap.get(pid);
            m.put("name",pp.getName());
            m.put("bannerUrl",pp.getBannerUrl());
            m.put("unitPrice",pp.getUnitPrice());
            m.put("shipping",pp.getShipping());
            m.put("inventory",pp.getInventory());
            m.put("status",pp.getStatus());
//            m.put("createTime",pp.getCreateTime());
            m.put("isFuxi",pp.getIsFuxi());
            m.put("isEveryday",pp.getIsEveryday());
            m.put("isConcentrate",pp.getIsConcentrate());
            m.put("isLimit",pp.getIsLimit());
            m.put("startTime",pp.getStartTime());
            m.put("endTime",pp.getEndTime());
            m.put("showInventory",pp.getInventory());
        }

        rm.put("list",idNumList);
        rm.put("total",num);
        rm.put("curPageNum",pageNum);
        int totalPageNum =(int)Math.ceil((double)num/pageSize);
        rm.put("totalPageNum",totalPageNum);
        
        return rm;
    }

    /**
     * 根据购物车ID修改购物车单个商品数量，加减一
     * @param userId
     * @param id
     * @param type  类型：1加一，其他减一
     * @return
     */
    @Override
    public boolean changeNumOfProductInTrolley(int userId,int id, int type) {
        int step=type==1?1:-1;
        return trolleyBasisDomain.increaseOrDecreaseByOne(id,step)==1;
    }
    
}
